package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author yiWen
 * @Date 2025/1/511:18
 */
@Component
public class SprinRabbitListener {
    /**
     * 简单模型-消费者
     *
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void simpleQueueListener(String msg) {
        System.out.println("消费者接收到消息：" + msg);
    }

    @RabbitListener(queues = "work.queue")
    public void workQueueListener1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：" + msg);
        TimeUnit.MILLISECONDS.sleep(10);
    }

    @RabbitListener(queues = "work.queue")
    public void workQueueListener2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到消息：" + msg);
        TimeUnit.MILLISECONDS.sleep(100);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void fanoutQueue1Listener(String msg) {
        System.out.println("消费者1接收到消息：" + msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void fanoutQueue2Listener(String msg) {
        System.out.println("消费者2接收到消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "direct.exchange", type = ExchangeTypes.DIRECT), key = {"blue","red"}))
    public void directQueueListener(String msg) {
        System.out.println("消费者接收到消息：" + msg);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
            exchange =@Exchange(name = "direct.exchange", type = ExchangeTypes.DIRECT),
            key = {"yellow","red"}
    ))
    public void directQueueListener2(String msg) {
        System.out.println("消费者接收到消息：" + msg);
    }

}
